<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - coverage.lcov - src/connectionhandling/mqtt_client_mqtt_connection.dart</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">src/connectionhandling</a> - mqtt_client_mqtt_connection.dart<span style="font-size: 80%;"> (source / <a href="mqtt_client_mqtt_connection.dart.func.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">coverage.lcov</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">17</td>
            <td class="headerCovTableEntry">28</td>
            <td class="headerCovTableEntryLo">60.7 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2017-10-09</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntryHi">-</td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /*</a>
<span class="lineNum">       2 </span>            :  * Package : mqtt_client
<span class="lineNum">       3 </span>            :  * Author : S. Hamblett &lt;steve.hamblett@linux.com&gt;
<span class="lineNum">       4 </span>            :  * Date   : 22/06/2017
<span class="lineNum">       5 </span>            :  * Copyright :  S.Hamblett
<span class="lineNum">       6 </span>            :  */
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : part of mqtt_client;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : /// State and logic used to read from the underlying network stream.
<span class="lineNum">      11 </span>            : class ReadWrapper {
<span class="lineNum">      12 </span>            :   /// Creates a new ReadWrapper that wraps the state used to read a message from a stream.
<span class="lineNum">      13 </span><span class="lineCov">          3 :   ReadWrapper() {</span>
<span class="lineNum">      14 </span><span class="lineCov">          6 :     this.messageBytes = new List&lt;int&gt;();</span>
<span class="lineNum">      15 </span>            :   }
<span class="lineNum">      16 </span>            : 
<span class="lineNum">      17 </span>            :   /// The bytes associated with the message being read.
<span class="lineNum">      18 </span>            :   List&lt;int&gt; messageBytes;
<span class="lineNum">      19 </span>            : }
<span class="lineNum">      20 </span>            : 
<span class="lineNum">      21 </span>            : /// The MQTT connection base class
<span class="lineNum">      22 </span>            : class MqttConnection extends Object with events.EventEmitter {
<span class="lineNum">      23 </span>            :   /// The socket that maintains the connection to the MQTT broker.
<span class="lineNum">      24 </span>            :   dynamic client;
<span class="lineNum">      25 </span>            : 
<span class="lineNum">      26 </span>            :   /// The read wrapper
<span class="lineNum">      27 </span>            :   ReadWrapper readWrapper;
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span>            :   /// Indicates if disconnect(onDone) has been requested or not
<span class="lineNum">      30 </span>            :   bool disconnectRequested = false;
<span class="lineNum">      31 </span>            : 
<span class="lineNum">      32 </span>            :   /// Default constructor
<span class="lineNum">      33 </span><span class="lineCov">          3 :   MqttConnection();</span>
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            :   /// Initializes a new instance of the MqttConnection class.
<span class="lineNum">      36 </span><span class="lineNoCov">          0 :   MqttConnection.fromConnect(String server, int port) {</span>
<span class="lineNum">      37 </span><span class="lineNoCov">          0 :     connect(server, port);</span>
<span class="lineNum">      38 </span>            :   }
<span class="lineNum">      39 </span>            : 
<span class="lineNum">      40 </span>            :   /// Connect, must be overridden in connection classes
<span class="lineNum">      41 </span>            :   Future connect(String server, int port) {
<span class="lineNum">      42 </span><span class="lineNoCov">          0 :     final Completer completer = new Completer();</span>
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :     return completer.future;</span>
<span class="lineNum">      44 </span>            :   }
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            :   /// Create the listening stream subscription and subscribe the callbacks
<span class="lineNum">      47 </span>            :   void _startListening() {
<span class="lineNum">      48 </span><span class="lineCov">          3 :     MqttLogger.log(&quot;MqttConnection::_startListening&quot;);</span>
<span class="lineNum">      49 </span><span class="lineCov">         15 :     client.listen(_onData, onError: _onError, onDone: _onDone);</span>
<span class="lineNum">      50 </span>            :   }
<span class="lineNum">      51 </span>            : 
<span class="lineNum">      52 </span>            :   /// OnData listener callback
<span class="lineNum">      53 </span>            :   void _onData(List&lt;int&gt; data) {
<span class="lineNum">      54 </span><span class="lineCov">          3 :     MqttLogger.log(&quot;MqttConnection::_onData&quot;);</span>
<span class="lineNum">      55 </span>            :     // Protect against 0 bytes but should never happen.
<span class="lineNum">      56 </span><span class="lineCov">          6 :     if (data.length == 0) {</span>
<span class="lineNum">      57 </span>            :       return;
<span class="lineNum">      58 </span>            :     }
<span class="lineNum">      59 </span><span class="lineCov">          9 :     readWrapper.messageBytes.addAll(data);</span>
<span class="lineNum">      60 </span>            :     // Attempt to create a message, if this works we have a full message
<span class="lineNum">      61 </span>            :     // if not add the bytes to the read wrapper and wait for more bytes.
<span class="lineNum">      62 </span>            :     bool messageIsValid = true;
<span class="lineNum">      63 </span>            :     MqttMessage msg;
<span class="lineNum">      64 </span>            :     try {
<span class="lineNum">      65 </span><span class="lineCov">          3 :       final MqttByteBuffer messageStream = new MqttByteBuffer.fromList(data);</span>
<span class="lineNum">      66 </span><span class="lineCov">          3 :       msg = MqttMessage.createFrom(messageStream);</span>
<span class="lineNum">      67 </span>            :     } catch (exception) {
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :       MqttLogger.log(&quot;MqttConnection::_ondata - message is not valid&quot;);</span>
<span class="lineNum">      69 </span>            :       messageIsValid = false;
<span class="lineNum">      70 </span>            :     }
<span class="lineNum">      71 </span>            :     if (messageIsValid) {
<span class="lineNum">      72 </span><span class="lineCov">          6 :       MqttLogger.log(&quot;MqttConnection::_onData - message received $msg&quot;);</span>
<span class="lineNum">      73 </span><span class="lineCov">          6 :       emitEvent(new MessageAvailable(msg));</span>
<span class="lineNum">      74 </span><span class="lineCov">          3 :       MqttLogger.log(&quot;MqttConnection::_onData - message processed&quot;);</span>
<span class="lineNum">      75 </span>            :     }
<span class="lineNum">      76 </span>            :   }
<span class="lineNum">      77 </span>            : 
<span class="lineNum">      78 </span>            :   /// OnError listener callback
<span class="lineNum">      79 </span>            :   void _onError(error) {
<span class="lineNum">      80 </span><span class="lineCov">          2 :     _disconnect();</span>
<span class="lineNum">      81 </span>            :   }
<span class="lineNum">      82 </span>            : 
<span class="lineNum">      83 </span>            :   /// OnDone listener callback
<span class="lineNum">      84 </span>            :   void _onDone() {
<span class="lineNum">      85 </span>            :     // We should never be done unless requested
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :     _disconnect();</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :     if (!disconnectRequested) {</span>
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :       throw new SocketException(</span>
<span class="lineNum">      89 </span>            :           &quot;MqttConnection::On Done called by broker, disconnecting.&quot;);
<span class="lineNum">      90 </span>            :     }
<span class="lineNum">      91 </span>            :   }
<span class="lineNum">      92 </span>            : 
<span class="lineNum">      93 </span>            :   /// Disconnects from the message broker
<span class="lineNum">      94 </span>            :   void _disconnect() {
<span class="lineNum">      95 </span><span class="lineCov">          2 :     if (client != null) {</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :       client.close();</span>
<span class="lineNum">      97 </span>            :     }
<span class="lineNum">      98 </span>            :   }
<span class="lineNum">      99 </span>            : 
<span class="lineNum">     100 </span>            :   /// Sends the message in the stream to the broker.
<span class="lineNum">     101 </span>            :   void send(MqttByteBuffer message) {
<span class="lineNum">     102 </span><span class="lineCov">          6 :     final typed.Uint8Buffer messageBytes = message.read(message.length);</span>
<span class="lineNum">     103 </span><span class="lineCov">          9 :     client.add(messageBytes.toList());</span>
<span class="lineNum">     104 </span>            :   }
<span class="lineNum">     105 </span>            : 
<span class="lineNum">     106 </span>            :   // User requested disconnection
<span class="lineNum">     107 </span>            :   void disconnect() {
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :     disconnectRequested = true;</span>
<span class="lineNum">     109 </span><span class="lineNoCov">          0 :     _onDone();</span>
<span class="lineNum">     110 </span>            :   }
<span class="lineNum">     111 </span>            : }
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.10</a></td></tr>
  </table>
  <br>

</body>
</html>
